package org.uim.cricket.analyzer.parsers.misc.impl;

import org.uim.cricket.analyzer.parsers.misc.URLIDParsable;

/**
 * Parses a URL and gets the ID within it. Example formats of
 * input format can be either,
 * <code>"ci/content/player/42605.html"</code>,
 * <code>"ci/content/player/42605"</code> or
 * <code>"42605.html"</code>.
 * In both ways it returns a value of "42605", otherwise
 * it returns input string.
 * 
 * @author Isuru Madushanka
 *
 */
public class NumberURLIDParser implements URLIDParsable {

	@Override
	public String parseUrlID(String source) {
		if (source == null || source.length() == 0) return source;
		
		int dashpos = source.lastIndexOf('/');
		int dotpos = source.lastIndexOf('.');
		
		if (dashpos < dotpos && dashpos >= 0) {
			return source.substring(dashpos+1, dotpos);
		} else if (dashpos > dotpos) {
			return source.substring(dashpos+1).trim();
		} else if (dotpos > dashpos && dashpos < 0) {
			return source.substring(0,dotpos).trim();
		}
		return source;
	}

}
